﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Labb3.View
{
    class BallView
    {
        private Model.BallSimulation _ballSimulation;
        public Camera _camera;

        private Texture2D _ballTexture;
        private Texture2D _arenaTexture;
        private Texture2D _mouseTexture;
        private int _resolutionX;
        private int _resolutionY;

        public BallView(Model.BallSimulation ballSimulation, GraphicsDeviceManager gdm, int resolutionX = 640, int resolutionY = 640)
        {
            _ballSimulation = ballSimulation;
            _camera = new Camera(resolutionX, resolutionY);
            _resolutionX = resolutionX;
            _resolutionY = resolutionY;

            _camera.resize(gdm);
        }

        public void LoadContent(ContentManager contentManager)
        {
            _ballTexture = contentManager.Load<Texture2D>("ball");
            _arenaTexture = contentManager.Load<Texture2D>("ram");
            _mouseTexture = contentManager.Load<Texture2D>("DiscoCrosshair");
        }

        public void Draw(SpriteBatch spritebatch)
        {
            float offsetX = _camera._offsetX;
            float offsetY = _camera._offsetY;
            Rectangle arenaRect = new Rectangle((int)offsetX / 2, (int)offsetY / 2,
                (int)(_camera.GetVisualCoords(new Vector2(1, 1), true).X),
                (int)(_camera.GetVisualCoords(new Vector2(1, 1), true).Y));
            spritebatch.Draw(_arenaTexture, arenaRect, Color.White);
            //spritebatch.Draw(_arenaTexture, new Rectangle(0, 0, _resolutionX, _resolutionY), Color.White); 
                             
            foreach(Model.Ball ball in _ballSimulation._ballList)
            {
                Vector2 visualCoords = _camera.GetVisualCoords(ball._position, true);
                int ballSizeX = (int)(ball._diameter * (_resolutionX - offsetX));
                int ballSizeY = (int)(ball._diameter * (_resolutionY - offsetY));
                Rectangle ballRect = new Rectangle((int)(visualCoords.X + offsetX / 2),
                    (int)(visualCoords.Y + offsetY / 2),
                    ballSizeX, ballSizeY);
                spritebatch.Draw(_ballTexture, ballRect, Color.White);
            }

            Vector2 mousePos = _camera.GetVisualCoords(_ballSimulation._mouseRect.TopLeft);
            Vector2 mouseSize = new Vector2(_ballSimulation._mouseRect.Size);
            int rectSizeX = (int)_camera.GetVisualCoords(mouseSize).X;
            int rectSizeY = (int)_camera.GetVisualCoords(mouseSize).Y;
            //Rectangle mouseRect = new Rectangle((int)mousePos.X - rectSizeX / 2, (int)mousePos.Y - rectSizeY / 2, rectSizeX, rectSizeY);
            Rectangle mouseRect = new Rectangle((int)mousePos.X, (int)mousePos.Y, rectSizeX, rectSizeY);
            spritebatch.Draw(_mouseTexture, mouseRect, Color.White);

            
            
        }
    }
}
